<?
	require_once('init.php');
	require_once('dataModel.php');	  
	require_once('cartClass.php');
 
 	if(!isset($_SESSION['cart'])) 
 	{ 
		$_SESSION['cart'] =serialize(new Cart()); 
	}
    
    $iType=1;
    if( isset( $_REQUEST['iType'] )) 
    {
 	  	$iType=$_REQUEST['iType']; 	 
  	} 
  	else 
  	{
  		echo 0;
  		exit;
  	} 
  
  	$cartItem=unserialize($_SESSION['cart']);
  	
  	
 // $cartItem->Cart();
	switch ($iType)
	{
		case 1; //增加
			$iGID="";
			if( isset( $_REQUEST['GID'] )) 
			{
				$iGID=$_REQUEST['GID']; 	 
			} 
			else 
			{
				echo -1; //参数不正确
				exit;
            }
				
			$iCID="";
			if( isset( $_REQUEST['CID'] )) 
			{
				$iCID=$_REQUEST['CID']; 	 
			} 
			else 
			{
 			 	echo -1; //参数不正确
			 	exit;
			}
			
			
			$iSID="";
			if( isset( $_REQUEST['SID'] )) 
			{
				$iSID=$_REQUEST['SID']; 	 
			} 
			else 
			{
			  	echo -1; //参数不正确
				exit;
			}
			
			
			$iNum=0;
			if( isset( $_REQUEST['NUM'] )) 
			{
				$iNum=$_REQUEST['NUM']; 	
				if ($iNum<=0) {$iNum=1;}
			} 
			else 
			{
				echo -1; //参数不正确
				exit;
			}			     
			
			
			$iPrice=0; //用函数得到
			$iName="测试一"; //用函数得到
			$iKeyID=$iGID."^".$iCID."^".$iSID; 
			$gItem=array("$iKeyID","$iName","$iPrice" ,"$iNum");
			$cartItem->add($gItem);
			$items=$cartItem->getCart();
					   
			$_SESSION['cart'] =serialize($cartItem);
			setcookie('cart',serialize($cartItem),time()+864000);
			echo 1; //添加成功
			exit;    			     
			break;
			
		case 2;//删除
			$iGID="";
			if( isset( $_REQUEST['GID'] )) 
			{
				$iGID=$_REQUEST['GID']; 	 
			} 
			
			
			$iCID="";
			if( isset( $_REQUEST['CID'] )) 
			{
				$iCID=$_REQUEST['CID']; 	 
			}  
			
			
			$iSID="";
			if( isset( $_REQUEST['SID'] )) 
			{
				$iSID=$_REQUEST['SID']; 	 
			}  
			
			
			$iNum=0;
			if( isset( $_REQUEST['NUM'] )) 
			{
				$iNum=$_REQUEST['NUM']; 	
				if ($iNum<=0) { $iNum=1;}
			}  
			
			
			if ($iGID=="")		//清空购物车		     
			{
				$iKeyID="";
			} 
			else 
			{
			 	$iKeyID=$iGID."^".$iCID."^".$iSID; 
			}
			
			
			$cartItem->remove($iKeyID,$iNum);	
			$_SESSION['cart'] =serialize($cartItem);	
			setcookie('cart',serialize($cartItem),time()+864000);
			echo 1; //添加成功
			exit;    			     					     
						
			break;
					
		case 3;//修改数量
			$iGID="";
			if( isset( $_REQUEST['GID'] )) 
			{
				$iGID=$_REQUEST['GID']; 	 
			} 
			else 
			{
				echo -1; //参数不正确
				exit;
			}
			
			$iCID="";
			if( isset( $_REQUEST['CID'] )) 
			{
				$iCID=$_REQUEST['CID']; 	 
			} 
			else 
			{
				echo -1; //参数不正确
				exit;
			}
			
			
			$iSID="";
			if( isset( $_REQUEST['SID'] )) 
			{
				$iSID=$_REQUEST['SID']; 	 
			} 
			else 
			{
				echo -1; //参数不正确
				exit;
			}
			
			
			$iNum=0;
			if( isset( $_REQUEST['NUM'] )) 
			{
				$iNum=$_REQUEST['NUM']; 	
				if ($iNum<=0) { $iNum=1;}
			}  
			
			$iKeyID=$iGID."^".$iCID."^".$iSID; 
			
			$cartItem->modi($iKeyID,$iNum);		
			$_SESSION['cart'] =serialize($cartItem);
			setcookie('cart',serialize($cartItem),time()+864000);
			echo 1; //修改成功
			exit;    			     					     
			break;						

		case 4;//得到购物车并列表
			$Items=$cartItem->getCart();
			//循环得到html
			$sHtml="";
			foreach($Items as $iItem)
			{
				$iKeyIDs=$iItem['ID']; //对其分解
				$iKeyID= explode("^",$iKeyIDs);
				$iGID=$iKeyID[0];
				$iCID=$iKeyID[1];
				$iSID=$iKeyID[2];
				$iName=$iItem['name'];//应该根据语言动态取得，用函数
				$iPrice=$iItem['price'];
				$count=$iItem['count'];
				$sHtml.=$iGID.",".$iCID.",".$iSID.",".$iPrice.",".$count."</br>";			 	 
			} 
			echo $sHtml ;
			exit; 
			break;			 

		default:
			echo 0;
			exit;
	}  
?>